package org.cqu_proj.demo.controller;

import org.cqu_proj.demo.dto.ResponseResult;
import org.cqu_proj.demo.entity.Movie;
import org.cqu_proj.demo.entity.Rating;
import org.cqu_proj.demo.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Page;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@RestController
@RequestMapping("/movies")
public class MovieController {

    @Autowired
    private MovieService movieService;

    // 分页查询电影
    @GetMapping("/getMovies")
    public ResponseResult<Page<Movie>> getMoviesWithRating(@RequestParam int page, @RequestParam int size) {
        Page<Movie> moviePage = movieService.getMovies(page, size);
        return ResponseResult.success(moviePage);
    }
    //评论电影
    @GetMapping("/clickRating")
    public ResponseResult<Rating> rateMovie(@RequestParam("newId") int userId,@RequestParam("movieId") int movieId,@RequestParam("rating") Double rating){
        ResponseResult<Rating> result=this.movieService.RatingMovie(userId,movieId,rating);
        if(result.getCode()==203) return ResponseResult.failure(201,"评论的电影不存在了");
        else return  ResponseResult.success(result.getData());
    }
    // <editor-fold desc="电影评分推荐">
    // 根据 genres 筛选并分页查询
    @GetMapping("/moviesByGenre")
    public ResponseResult<List<Movie>> getMoviesByGenres(@RequestParam String genres, @RequestParam int page, @RequestParam int size) {
        System.out.println("这是输入的genres:"+genres);
        List<String>listGenres=getGenreList(genres);
        return movieService.getMoviesByGenresAndSortedByRating(listGenres, page, size);
    }

    // 查询所有电影按评分排序分页
    @GetMapping("/recommendByRating")
    public ResponseResult<List<Movie>> getAllMovies(@RequestParam int page, @RequestParam int size) {
        return movieService.getAllMoviesSortedByRating(page, size);
    }
    //字符转换成数组
    public List<String> getGenreList(String genres) {
        if (genres != null) {
            List<String>temp= Arrays.asList(genres.split(","));
            System.out.println("Genres as List: " + temp);
            return temp;
        }

        return Collections.emptyList();
    }
    // </editor-fold>
    // <editor-fold desc="genres与矩阵">
    @GetMapping("/clickGenres")
    public  ResponseResult<List<Movie>>  clickGenres(@RequestParam("genres") String genres,@RequestParam("userId") int userId){
        System.out.println("这是输入的genres:"+genres);
        List<String>listGenres=getGenreList(genres);
        return  this.movieService.clickGenres(userId,genres,listGenres);

    }
    @GetMapping( "/clickMovie")
    public  ResponseResult<String>  clickMovie(@RequestParam("genres") String genres,@RequestParam("userId") int userId){
        System.out.println("这是输入的genres:"+genres);
        return  this.movieService.clickMovie(userId,genres);
    }
    //上述的分页
    @GetMapping("genresPage")
    public  ResponseResult<List<Movie>> genresPage(String genres,int page,int size){
        System.out.println("这是输入的genres:"+genres);
        List<String>listGenres=getGenreList(genres);
        return movieService.getMoviesByGenresAndSortedByRating(listGenres, page, size);
    }
    // </editor-fold>

    //电影推荐
    @GetMapping("recommendMovies")
    public  ResponseResult<List<Movie>> recommendMovies(int userId,int page,int size){
        return this.movieService.recommendMovie(userId,page,size);
    }
}
